package cn.gxufe.reliable.message.admin.service.impl;

import cn.gxufe.reliable.message.admin.domain.RMessage;
import cn.gxufe.reliable.message.admin.schedule.consumer.TryFinishOrCancelService;
import cn.gxufe.reliable.message.admin.schedule.mq.SendMessageToMqService;
import cn.gxufe.reliable.message.admin.schedule.prodiucer.CancelService;
import cn.gxufe.reliable.message.admin.schedule.prodiucer.FinishService;
import cn.gxufe.reliable.message.admin.schedule.prodiucer.TryConfirmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static cn.gxufe.reliable.message.admin.domain.RMessage.STATUS_TRY_FINISH;
import static cn.gxufe.reliable.message.common.Message.*;


/**
 * @author yignjjie.rong
 */
@Component
public class MessageEventHandler  {

    @Autowired
    private TryConfirmService tryConfirmService;

    @Autowired
    private SendMessageToMqService sendMessageToMqService;

    @Autowired
    private FinishService finishService;

    @Autowired
    private TryFinishOrCancelService tryFinishOrCancelService;

    @Autowired
    private CancelService cancelService;



    public void onEvent(RMessage event) {
        switch (event.getStatus()){
            case STATUS_PREPARE:
                // 回调确认 confirm
                tryConfirmService.tryConfirm(event);
                break;
            case STATUS_CONFIRM:
                // 发送数据到kafka
                sendMessageToMqService.sendAndUpdateTime(event);
                break;
            case STATUS_TRY_FINISH:
                // 调用下游服务，确认finish or cancel
                tryFinishOrCancelService.tryFinishOrCancel(event);
                break;
            case STATUS_FINISH:
                // 回调上游
                finishService.finish(event);
                break;
            case STATUS_CANCEL:
                // 回调上游
                cancelService.cancel(event);
                break;
                default:
                    throw new IllegalStateException("not support status : " + event.getStatus());
        }
    }

}  
